package com.beg.cant.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.beg.cant.comment.result.Result;
import com.beg.cant.comment.utils.WechatSecurityHelper;
import com.beg.cant.dto.PageRequest;
import com.beg.cant.entity.Comment;
import com.beg.cant.service.CommentService;
import com.beg.cant.vo.CommentsVO;
import com.beg.cant.dto.CommentPageRequest;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.lang.Long;
import java.time.LocalDateTime;
import java.util.List;
@Slf4j
@RestController
@RequestMapping("/api/comments")
@Tag(name = "评论管理", description = "评论的增删改查接口")
public class CommentController {
    @Autowired
    private CommentService commentService;
    @Autowired
    private WechatSecurityHelper wechatSecurityHelper;
    @PostMapping
    @Operation(summary = "创建评论", description = "创建一个新的评论")
    public Result<String> createComment(@RequestBody Comment comment) throws IOException {
        wechatSecurityHelper.checkContent(comment.getContent(),null);
        comment.setCreatedAt(LocalDateTime.now());
        commentService.save(comment);
        return Result.success("评论创建成功");
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取评论", description = "根据动态ID获取评论信息")
    public Result<List<CommentsVO>> getComment(@Parameter(description = "动态ID") @PathVariable Long id) {
        return Result.success(commentService.getPostsByIdComment(id));
    }

    @PostMapping("/list")
    @Operation(summary = "获取所有评论", description = "分页获取所有评论的列表")
    public Result<Page<Comment>> getAllComments(@RequestBody PageRequest request) {
        log.info("评论{}",request);
        Page<Comment> page = new Page<>(request.getPageNum(), request.getPageSize());
        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
        if (request.getCode() != null) {
            wrapper.like(Comment::getContent,request.getCode());
        }
        wrapper.orderByAsc(Comment::getCreatedAt);
        if (request.getStartTime() != null) {
            wrapper.ge(Comment::getCreatedAt,request.getStartTime());
        }
        if (request.getEndTime() != null) {
            wrapper.le(Comment::getCreatedAt,request.getEndTime());
        }
        return Result.success(commentService.page(page,wrapper));
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新评论", description = "根据ID更新评论信息")
    public Result<String> updateComment(
            @RequestBody Comment comment) {
        commentService.updateById(comment);
        return Result.success("评论更新成功");
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除评论", description = "根据ID删除评论")
    public Result<String> deleteComment(@Parameter(description = "评论ID") @PathVariable Long id) {
        commentService.removeById(id);
        return Result.success("评论删除成功");
    }
}
